package com.plian.system.controller.zcgl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.entity.zcgl.JzsgjgbaEntity;
import com.plian.system.entity.zp.ZpPlan;
import com.plian.system.entity.zp.ZpPlanSituation;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.zcgl.IJzsgjgbaService;
import com.plian.system.vo.zcgl.JzsgjgbaVO;
import com.plian.system.vo.zp.ZpPlanSituationVO;
import com.plian.system.wrapper.util.CacheWrapperUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 竞争上岗结果备案
 */
@RestController
@RequestMapping("api/jzsgjgba")
public class JzsgjgbaController extends BaseController {
    private final IJzsgjgbaService service;
    private final CacheWrapperUtil cacheWrapperUtil;

    public JzsgjgbaController(IJzsgjgbaService service, CacheWrapperUtil cacheWrapperUtil) {
        this.service = service;
        this.cacheWrapperUtil = cacheWrapperUtil;
    }

    /**
     * 详情
     */
    @PostMapping("/detail")
    public R<JzsgjgbaVO> detail(@RequestBody JzsgjgbaEntity param) {
        JzsgjgbaEntity entity = service.getOne(Condition.getQueryWrapper(param));
        JzsgjgbaVO vo = new JzsgjgbaVO();
        BeanUtil.copyProperties(entity, vo);
        vo.setCreateUserName(cacheWrapperUtil.userIdToName(entity.getCreateUser()));
        vo.setApprovedUserName(cacheWrapperUtil.userIdToName(entity.getApprovedUser()));
        vo.setUpdateUserName(cacheWrapperUtil.userIdToName(entity.getUpdateUser()));
        vo.setReportCompanyName(cacheWrapperUtil.orgIdToName(entity.getReportCompanyId()));
        return R.data(vo);
    }

    /**
     * 列表
     *
     * @return
     */
    @PostMapping("/list")
    public ListResult<JzsgjgbaVO> list(@RequestBody Query query) {
        List<SearchEntity> entityList = query.getSearch();
        if (query.getAscs() == null && query.getDescs() == null) {
            query.setDescs("create_time");
        }
        QueryWrapper<JzsgjgbaEntity> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new JzsgjgbaEntity()), entityList
        );
        TokenSearch<JzsgjgbaEntity> tokenSearch = new TokenSearch<>();
        tokenSearch.addTokenQuery(queryWrapper, query);

        IPage<JzsgjgbaEntity> pages = service.page(Condition.getPage(query), queryWrapper);
        ListResult<JzsgjgbaVO> listResult = new ListResult<>(CommonCode.SUCCESS);
        listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
        List<JzsgjgbaVO> vos = new ArrayList<>();
        pages.getRecords().forEach(entity -> {
            JzsgjgbaVO vo = new JzsgjgbaVO();
            BeanUtil.copyProperties(entity, vo);
            vo.setCreateUserName(cacheWrapperUtil.userIdToName(entity.getCreateUser()));
            vo.setApprovedUserName(cacheWrapperUtil.userIdToName(entity.getApprovedUser()));
            vo.setUpdateUserName(cacheWrapperUtil.userIdToName(entity.getUpdateUser()));
            vo.setReportCompanyName(cacheWrapperUtil.orgIdToName(entity.getReportCompanyId()));
            vos.add(vo);
        });
        listResult.setList(vos);
        return listResult;
    }

    /**
     * 新增
     *
     * @return
     */
    @PostMapping("/add")
    @LogOperation("竞争上岗结果备案-保存")
    public R add(@RequestBody JzsgjgbaEntity jzsgjgba) {
        return R.status(service.add(jzsgjgba));
    }

    /**
     * 修改
     *
     * @return
     */
    @PostMapping("/edit")
    @LogOperation("竞争上岗结果备案-编辑")
    public R edit(@RequestBody JzsgjgbaEntity jzsgjgba) {
        return R.status(service.edit(jzsgjgba));
    }

    /**
     * 删除
     *
     * @return
     */
    @PostMapping("/remove")
    @LogOperation("竞争上岗结果备案-删除")
    public R remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>) pd.get("ids");
        return R.status(service.remove(Func.toLongList(StringUtils.join(ids, StringPool.COMMA))));
    }
}
